package com.asia5b.a5mall.middleware.logic.viewmodel.cart

import androidx.lifecycle.MutableLiveData
import com.asia5b.a5mall.middleware.R
import com.asia5b.a5mall.middleware.base.SimpleNetParams
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.config.UserRepository
import com.asia5b.a5mall.middleware.model.cart.CartAllModel
import com.asia5b.a5mall.middleware.model.member.CouponModel
import com.asia5b.a5mall.middleware.utils.judge
import com.asia5b.a5mall.middleware.utils.showMessage
import io.reactivex.functions.Consumer

/**
 * Created by zhangyuncai on 2020/8/19.
 */
class ShopCartFragmentVM:SimpleViewModel() {


    val loadCartDataLiveData=MutableLiveData<CartAllModel>()
    /**
     * 加载购物车列表
     */
    fun loadCartData(refresh: Int) {
        if (!UserRepository.isLogin()) {
//            loadMvpView.dismissLoadingBar()
            loadingLiveData.value=1
            return
        }

        baseSubscribe(cartApi.getCartData("all"), SimpleNetParams(isShowLoading = (refresh != 1)), Consumer { result ->
//            providerView().showCartView(result)
            loadCartDataLiveData.value=result
        })
    }

    /**
     * 对商品编辑
     */
    fun editItem(productId: Int, checked: Boolean?, num: Int?) {
        var check: Int? = null
        if (checked != null) {
            check = checked.judge(1, 0)
        }
        val observable = cartApi.updateItemState("$productId", check, num)
                .map { return@map true }
        baseSubscribe(observable, Consumer { result ->
            if (result) {
                loadCartData(1)
            }
        })
    }

    /**
     * 对店铺编辑
     */
    fun shopCheck(shopId: Int, checked: Boolean) {
        val observable = cartApi.updateSellerGoods(shopId, checked.judge(1, 0))
                .map { return@map true }
        baseSubscribe(observable, Consumer { result ->
            if (result) {
                loadCartData(1)
            }
        })
    }

    /**
     * 删除多个商品
     */
    fun deleteGoods(productIds: String) {
        val observable = cartApi.delete("$productIds")
                .map { return@map true }
        baseSubscribe(observable, Consumer { result ->
            if (result) {
                showMessage(R.string.删除成功)
                loadCartData(1)
            }
        })
    }

    /**
     * 复选框
     */
    fun allCheck(check: Boolean) {
        val observable = cartApi.setAllCheck(check.judge(1, 0))
                .map { true }
        baseSubscribe(observable, Consumer { result ->
            if (result) {
                loadCartData(1)
            }
        })
    }

    /**
     * @Note   关注收藏商品
     * @param  goodsId 商品ID
     */
    fun collectGoods(goodsId: Int) {
        val observable = memberApi.addCollection(goodsId)
        baseSubscribe(observable, Consumer { data ->
            showMessage(R.string.关注成功)
        })
    }

    val getCouponListLiveData=MutableLiveData<ArrayList<CouponModel>>()
    fun getCouponList(seller_id: Int) {
        baseSubscribe(promotionApi.getSellerCoupon(seller_id), Consumer {
//            providerView().getCouponListResult(it)
            getCouponListLiveData.value=it
        })
    }


}